package com.tencent.miniproject.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tencent.miniproject.R;
import com.tencent.miniproject.adapter.UserBillAdapter;
import com.tencent.miniproject.base.BaseFragment;
import com.tencent.miniproject.been.Post;
import com.tencent.miniproject.constants.Constant;
import com.tencent.miniproject.fragment.i.IUserBillFragment;
import com.tencent.miniproject.presenter.UserBillPresenter;
import com.tencent.miniproject.presenter.i.IUserBillPresenter;
import com.tencent.miniproject.ui.DetailActivity;
import com.tencent.miniproject.ui.MainActivity;
import com.tencent.miniproject.ui.MapActivity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by PC on 2018/5/25.
 */

public class UserBillFragment extends BaseFragment implements IUserBillFragment,
        UserBillAdapter.UserBillInterface, AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.lv_user_bill)
    ListView lvUserBill;
    @BindView(R.id.srl_refresh)
    SwipeRefreshLayout srlRefresh;
    Unbinder unbinder;
    private ImageView mUserImgView;
    private TextView mUserNameTextView;
    private TextView mUserSpendTextView;    //本月消费

    //默认是我的账单页面
    private int mFragmentType = Constant.FRAGMENT_MINE_BILL;
    private List<Post> mUserBillList;   //用户账单数据
    private UserBillAdapter mUserBillAdapter;
    private IUserBillPresenter mPresenter;  //presenter
    private int mUserId;    //别人的账单，需要用户id


    public static UserBillFragment newInstance(int type) {
        UserBillFragment fragment = new UserBillFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int setLayoutResourceId() {
        return R.layout.fragment_user_bill;
    }

    @Override
    protected void initData(Bundle bundle) {
        mPresenter = new UserBillPresenter(this);
        //从服务器上获取数据，异步
        if (getArguments() != null) {
            mFragmentType = getArguments().getInt("type");
        }
        if (mFragmentType == Constant.FRAGMENT_OTHER_BILL) {
            //别人的页面
            mPresenter.getPostSummaryDataFromNet(mUserId);
        } else {
            //我的页面
            mPresenter.getPostSummaryDataFromNet();

        }
        mUserBillList = new ArrayList<>();
        mUserBillAdapter = new UserBillAdapter(mBaseActivity);
    }

    @Override
    protected void initView() {
        testData();
        mUserBillAdapter.updateData(mUserBillList);
        lvUserBill.setAdapter(mUserBillAdapter);
        lvUserBill.addHeaderView(addHeaderView());  //添加头部
    }

    /**
     * 添加头部
     *
     * @return view
     */
    public View addHeaderView() {
        View view = LayoutInflater.from(mBaseActivity).inflate(R.layout.layout_user_bill_header, null);
        mUserImgView = view.findViewById(R.id.iv_user_photo);
        mUserNameTextView = view.findViewById(R.id.tv_user_name);
        if (mFragmentType == Constant.FRAGMENT_MINE_BILL) {
            view.findViewById(R.id.ll_spend).setVisibility(View.VISIBLE);
            mUserSpendTextView = view.findViewById(R.id.tv_spend_money);
            mUserSpendTextView.setVisibility(View.VISIBLE);
        }
        return view;
    }

    void testData() {
        for (int i = 0; i < 10; i++) {
            Post neighborRecommand = new Post();
            neighborRecommand.setId(i);
            neighborRecommand.setImgUrl(Constant.imageUrls[i]);
            neighborRecommand.setStore("Wagaya 寿司店");
            neighborRecommand.setCost((long) 24.0);
            neighborRecommand.setPosition("深南大道980号");
            neighborRecommand.setReleaseTime(new Date());
            neighborRecommand.setVotes(100);
            neighborRecommand.setComments(300);
            mUserBillList.add(neighborRecommand);
        }

    }

    @Override
    protected void setListener() {
        mUserBillAdapter.setUserBillListener(this);
        lvUserBill.setOnItemClickListener(this);
        srlRefresh.setOnRefreshListener(this);
    }

    /**
     * 通知view层去presenter获取数据
     */
    @Override
    public void notifyDataChanged() {
        mUserBillAdapter.updateData(mPresenter.getPostList());
        Glide.with(mBaseActivity).load(mPresenter.getUserImgUrl()).into(mUserImgView);
        mUserNameTextView.setText(mPresenter.getUserName());
        if (Constant.FRAGMENT_MINE_BILL == mFragmentType) {
            //显示我的本月消费金额
            mUserSpendTextView.setText(String.valueOf("￥" + mPresenter.getSpendMoney()));
        }
    }

    @Override
    public void showToastInFragment(String string) {
        showToast(string);
    }

    /**
     * 点击位置，跳转到前端地图页面
     *
     * @param post 动态
     */
    @Override
    public void putPostIdToMapActivity(Post post) {
        Intent intent = new Intent();
        intent.setClass(mBaseActivity, MapActivity.class);
        intent.putExtra("postId", post.getId());
        try {
            intent.putExtra("lng", ((MainActivity)mBaseActivity).getLongitude());
            intent.putExtra("lat", ((MainActivity)mBaseActivity).getLatitude());
        }catch (Exception e){
            e.printStackTrace();
        }
        startActivity(intent);
    }

    /**
     * 点击评论、条目都跳转详情页
     *
     * @param post post
     */
    @Override
    public void jumpToDetailActivity(Post post) {
        Intent intent = new Intent();
        intent.setClass(mBaseActivity, DetailActivity.class);
        intent.putExtra("id", post.getId());
        intent.putExtra("longitude", post.getLongitude());
        intent.putExtra("latitude", post.getLatitude());
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i("ning", "onItemClick: " + position);
        if (position == 0) {
            return;
        }
        jumpToDetailActivity(mUserBillAdapter.getItem(position - 1));
    }

    /**
     * 别人的账单
     *
     * @param userId userId
     */
    public void setUserId(int userId) {
        this.mUserId = userId;
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        if (mFragmentType == Constant.FRAGMENT_OTHER_BILL) {
            mPresenter.getPostSummaryDataFromNet(mUserId);
        } else {
            mPresenter.getPostSummaryDataFromNet();
        }
        srlRefresh.setRefreshing(false);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constant.REFRESH_PUBLISH_SUCCESS_FINISH){
            //刷新页面
            if (srlRefresh == null){
                return;
            }
            srlRefresh.setRefreshing(true);
            onRefresh();
        }
    }
}
